package com.hui.forecastapp.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class DBManager {
    public static SQLiteDatabase database;

    // 初始化数据库信息
    public static void initDB(Context context) {
        DBHelper dbHelper = new DBHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    // 查找数据库当中省份列表
    public static List<String> queryAllProvinceName() {
        Cursor cursor = database.query("info", null, null, null, null, null, null);
        List<String> provinceList = new ArrayList<>();
        while (cursor.moveToNext()) {
            String province = cursor.getString(cursor.getColumnIndex("province"));
            provinceList.add(province);
        }
        return provinceList;
    }

    // 查找数据库当中城市列表
    public static List<String> queryAllCityName() {
        Cursor cursor = database.query("info", null, null, null, null, null, null);
        List<String> cityList = new ArrayList<>();
        while (cursor.moveToNext()) {
            String city = cursor.getString(cursor.getColumnIndex("city"));
            cityList.add(city);
        }
        return cityList;
    }

    // 根据城市名称替换信息内容
    public static  int updateInfoByCity(String city, String content) {
        ContentValues values = new ContentValues();
        values.put("content", content);
        return database.update("info", values, "city=?", new String[]{city});
    }

    // 添加新的城市
    public static int addCityInfo(String province, String city, String content) {
        ContentValues values = new ContentValues();
        values.put("province", province);
        values.put("city", city);
        values.put("content", content);
        return (int) database.insert("info", null, values);
    }

    // 根据城市名查询数据库当中的内容
    public static String queryInfoByCity(String city) {
        Cursor cursor = database.query("info", null, "city=?", new String[]{city}, null, null, null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            String content = cursor.getString(cursor.getColumnIndex("content"));
            return content;
        }
        return null;
    }

    // 获取目前已经存储的城市数量
    public static int getCityCount() {
        Cursor cursor = database.query("info", null, null, null, null, null, null);
        int count = cursor.getCount();
        return count;
    }

    // 查询获取全部信息
    public static List<DatabaseBean> queryAllInfo() {
        Cursor cursor = database.query("info", null, null, null, null, null, null);
        List<DatabaseBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String province = cursor.getString(cursor.getColumnIndex("province"));
            String city = cursor.getString(cursor.getColumnIndex("city"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            DatabaseBean bean = new DatabaseBean(id, province, city, content);
            list.add(bean);
        }
        return list;
    }

    // 根据城市名删除信息
    public static int deleteInfoByCity(String city) {
        return database.delete("info", "city=?", new String[]{city});
    }

    // 删除所有信息
    public static void deleteAllInfo() {
        String sql = "delete from info";
        database.execSQL(sql);
    }
}
